<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
				 com.scalar.chiptrack.users.Functionality,
				 com.scalar.chiptrack.commons.Constants"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<chiptrack:checkLogon sourcePage="/vendor.do"/>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>

<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type=text/css rel=stylesheet>

<META content="Microsoft FrontPage 5.0" name=GENERATOR></HEAD>
<BODY bgColor="#ffffff" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<SCRIPT language="JavaScript">

	<%
		if ( ( request.getAttribute("close") != null ) && ( ( ( String ) request.getAttribute("close") ).equalsIgnoreCase( "close" ) ) )
		{
	%>
		window.close();
	<%
		}
	%>


	function validateForm()
	{
		var error = '';
		var pa = document.getElementById("errors");
		pa.innerHTML = error;
		var form = document.vendorContactPersonForm;
		var count = parseInt( form.counter.value) -1;
		var flag = false;

		// Check for atleast one Default Address
		for( var x = 0; x < count ; x++ )
		{
			if ( form['contactPersonList['+ x +'].defaultAddress'].checked == true )
			{
				form['contactPersonList['+ x +'].active'].checked = true
				flag = true;
			}
		}

		/*if( flag == false )
		{
			error = 'Please select at least one Default contact person';
			pa.innerHTML = error;
			return false;
		}*/

		// Trim every field before checking further
		for( var x = 0; x < count ; x++ )
		{
			form['contactPersonList['+ x +'].contactPerson'].value	= form['contactPersonList['+ x +'].contactPerson'].value.trim();
			form['contactPersonList['+ x +'].email'].value			= form['contactPersonList['+ x +'].email'].value.trim();
			form['contactPersonList['+ x +'].phoneNumber'].value	= form['contactPersonList['+ x +'].phoneNumber'].value.trim();
			form['contactPersonList['+ x +'].faxNumber'].value		= form['contactPersonList['+ x +'].faxNumber'].value.trim();
			form['contactPersonList['+ x +'].cellNumber'].value		= form['contactPersonList['+ x +'].cellNumber'].value.trim();
			form['contactPersonList['+ x +'].address1'].value		= form['contactPersonList['+ x +'].address1'].value.trim();
			form['contactPersonList['+ x +'].address2'].value		= form['contactPersonList['+ x +'].address2'].value.trim();
			form['contactPersonList['+ x +'].address3'].value		= form['contactPersonList['+ x +'].address3'].value.trim();
			form['contactPersonList['+ x +'].address4'].value		= form['contactPersonList['+ x +'].address4'].value.trim();

			form['contactPersonList['+ x +'].city'].value			= form['contactPersonList['+ x +'].city'].value.trim();
			form['contactPersonList['+ x +'].state'].value			= form['contactPersonList['+ x +'].state'].value.trim();
			form['contactPersonList['+ x +'].zipCode'].value		= form['contactPersonList['+ x +'].zipCode'].value.trim();
			form['contactPersonList['+ x +'].country'].value		= form['contactPersonList['+ x +'].country'].value.trim();
		}


		// Check for Contact Person Name if the Contact Person is Active
		flag = true;
		for( var x = 0; x < count; x++ )
		{
			if ( form['contactPersonList['+ x +'].active'].checked == true )
			{
				if( form['contactPersonList['+ x +'].contactPerson'].value.length == 0 )
				{
					form['contactPersonList['+ x +'].contactPerson'].focus();
					flag = false;
				}
			}
		}

		/*if( flag == false )
		{
			error = 'Contact person name cannot be empty for active Contact persons';
			pa.innerHTML = error;
			return false;
		}*/

		// Check the Email Address of the Contact Person if given  ValidateMail
		flag = true;
		for( var x = 0; x < count; x++ )
		{
			if( form['contactPersonList['+ x +'].email'].value.length != 0 )
			{
				if ( ValidateMail( form['contactPersonList['+ x +'].email'].value ) == false )
				{
					form['contactPersonList['+ x +'].email'].focus();
					flag = false;
				}
			}
		}

		if( flag == false )
		{
			error = 'Please enter a vaild Email Address';
			pa.innerHTML = error;
			return false;
		}

		for( var x = 0; x < count; x++ )
		{
			var contactPerson	= form['contactPersonList['+ x +'].contactPerson'].value.trim();
			var email			= form['contactPersonList['+ x +'].email'].value.trim();
			var phoneNumber	    = form['contactPersonList['+ x +'].phoneNumber'].value.trim();
			var faxNumber		= form['contactPersonList['+ x +'].faxNumber'].value.trim();
			var cellNumber		= form['contactPersonList['+ x +'].cellNumber'].value.trim();
			var address1		= form['contactPersonList['+ x +'].address1'].value.trim();
			var address2		= form['contactPersonList['+ x +'].address2'].value.trim();
			var address3		= form['contactPersonList['+ x +'].address3'].value.trim();
			var address4		= form['contactPersonList['+ x +'].address4'].value.trim();
			var city			= form['contactPersonList['+ x +'].city'].value.trim();
			var state			= form['contactPersonList['+ x +'].state'].value.trim();
			var zipCode		    = form['contactPersonList['+ x +'].zipCode'].value.trim();
			var country		    = form['contactPersonList['+ x +'].country'].value.trim();

			if( contactPerson == '' && (email != '' ||  phoneNumber != '' || phoneNumber != '' || faxNumber != '' || cellNumber != '' || address1 != '' || address2 != '' || address3 != '' || address4 != '' || city != '' || state != '' || zipCode != '' || country != ''))
			{
                error = 'Contact person name cannot be empty';
                pa.innerHTML = error;
                return false;
			}
		}
		return true;
	}

	function assignValuesToParent()
	{
		var form = document.vendorContactPersonForm;
		var count = parseInt( form.counter.value) -1;
		var contactpersonName = '';
		var email = '';
		var Phone = '';
		var Fax = '';
		var CellNumber = '';

		for( var x = 0; x < count; x++ )
		{
			if ( form['contactPersonList['+ x +'].defaultAddress'].checked == true )
			{
			    var	i =1;
				contactpersonName = form['contactPersonList['+ x +'].contactPerson'].value;
				email = form['contactPersonList['+ x +'].email'].value;
				Phone = form['contactPersonList['+ x +'].phoneNumber'].value;
				Fax = form['contactPersonList['+ x +'].faxNumber'].value;
				CellNumber = form['contactPersonList['+ x +'].cellNumber'].value;
                opener.assignContactPerson( contactpersonName, email, Phone, Fax, CellNumber, form.type.value, i );
				break;
			}
		}
		i = 0;
	}

	function saveContactPerson()
	{
		if ( validateForm() == false )
		{
			return;
		}

		document.vendorContactPersonForm.action.value = "save";
		document.vendorContactPersonForm.submit();
		// Assign the Default Contact Person Name to Parent Window Contact Person
		assignValuesToParent();
		// window.close();
	}
	//Added by kumar 3/23/2004
	function cancelContactPerson()
	{
    	 window.close();
	}

	function addContactPerson()
	{
		document.vendorContactPersonForm.action.value = "addmore";
		document.vendorContactPersonForm.submit();
	}

	function setDefaultContact( index )
	{
		var form = document.vendorContactPersonForm;
		var count = parseInt( form.counter.value) -1;

		if( form['contactPersonList['+ ( index - 1 ) +'].defaultAddress'].checked == true )
		{
			for( var x = 0; x < count ; x++ )
			{
				if( ( index-1 ) != x )
				{
					form['contactPersonList['+ x +'].defaultAddress'].checked  = false;
				}
				else
				{
					form['contactPersonList['+ x +'].active'].checked  = true;
				}
			}
		}
	}
	function setActiveContact( index )
	{
		var form = document.vendorContactPersonForm;
		var count = parseInt( form.counter.value) -1;

		if( form['contactPersonList['+ ( index - 1 ) +'].active'].checked == false )
		{
			for( var x = 0; x < count ; x++ )
			{
				if( ( index-1 ) == x )
				{
					form['contactPersonList['+ x +'].defaultAddress'].checked  = false;
				}

			}
		}
	}

</SCRIPT>

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>

<TD bgcolor="#ffffff">
<IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
<TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
<TD valign="top" width="100%">

<IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
<BR>
<!-- from here your body starts -->


<center><span class="NArialW">Vendor Contact Persons </span></center>
<img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20>

<html:form  name="vendorContactPersonForm" type="com.scalar.chiptrack.ChipTrackActionForm" action="/vendorContactPerson.do" >

<table border="0" width="92%" align=right>
	<tr>
		<td colspan='5'>
		<FONT color='FF0000'> <logic:present name="errorMsg"><bean:write name="errorMsg" /></logic:present></FONT>
	    <chiptrack:errors  styleClass="NArialL" />
		</td>
	</tr>
	<tr><td colspan='5' align="left" class='NArialL'><FONT id="errors" color='FF0000'></FONT></TD></TR>
	<tr><td colspan='5'>&nbsp;</TD></TR>
	<html:hidden name="vendorContactPersonForm" property="action"/>
	<html:hidden name="vendorContactPersonForm" property="vendorId"/>
	<html:hidden name="vendorContactPersonForm" property="type"/>
	<% int i=1;%>
	<nested:iterate property="contactPersonList" type="com.scalar.chiptrack.commons.Address">
	<nested:hidden property="addressId"/>
	<!-- START -->
	<tr bgcolor="#DEE1E2" height="23">
		<td width="20%"><font class="NArialL" >Contact Person : <%=i%></font></td>
		<td align="right"><font class="NArialL" >Active: </font></td>
		<td>
		<% String setActiveContact = "javascript:setActiveContact("+ i +");"; %>
			<nested:checkbox property="active" onclick="<%=setActiveContact%>"/>
		</td>
		<td align="right"><font class="NArialL" >Default: </font></td>
		<% String setDefaultContact = "javascript:setDefaultContact("+ i +");"; %>
		<td><nested:checkbox property="defaultAddress" onclick="<%=setDefaultContact%>"/></td></font>
	</tr>

	<tr>
		<td width="20%" height="23"><font class="NArialL" >Contact Person :</font><font color="RED" size="1"><B>*</B></font></td>
		<td width="20%" height="23">
			<nested:text property="contactPerson" size="23" styleClass="txfield" maxlength="50" />
		</td>
		<td width="8%" height="23"></td>
	    <td width="20%" height="23"><font class="NArialL" >Email :</font> </td>
		<td width="32%" height="23">
			<nested:text property="email" size="23" styleClass="txfield" maxlength="50" />
		</td>
	</tr>
	<tr>
		<td width="20%" height="23"><font class="NArialL" >Phone :</font></td>
		<td width="20%" height="23">
			<nested:text property="phoneNumber" size="23" styleClass="txfield" onkeypress='validatePhoneNumber(this)'/>
		</td>
		<td width="8%" height="23"></td>
		<td width="20%" height="23"><font class="NArialL" >Fax :</font></td>
		<td width="32%" height="23">
  			<nested:text property="faxNumber" size="23" styleClass="txfield" onkeypress='validatePhoneNumber(this)'/>
		</td>
	</tr>
	<tr>
		<td width="20%" height="23"><font class="NArialL" >Cell Number :</font></td>
		<td width="20%" height="23">
			<nested:text property="cellNumber" size="23" styleClass="txfield" onkeypress='validatePhoneNumber(this)'/>
		</td>
		<td width="8%" height="23"></td>
		<td width="20%" height="23"><font class="NArialL" >&nbsp;</font></td>
		<td width="32%" height="23">&nbsp;</td>
	</tr>
	<tr>
		<td width="20%" height="23"><font class="NArialL" >Address 1 :</font></td>
		<td width="20%" height="23">
  			<nested:text property="address1" size="23" styleClass="txfield" maxlength="50" />
		</td>
		<td width="8%" height="23"></td>
	    <td width="20%" height="23"><font class="NArialL" >Address 2 :</font></td>
		<td width="32%" height="23">
		  	<nested:text property="address2" size="23" styleClass="txfield" maxlength="50" />
		</td>
	</tr>
	<tr>
		<td width="20%" height="23"><font class="NArialL" >Address 3 :</font></td>
		<td width="20%" height="23">
  			<nested:text property="address3" size="23" styleClass="txfield" maxlength="50" />
		</td>
		<td width="8%" height="23"></td>
	    <td width="20%" height="23"><font class="NArialL" >Address 4 :</font></td>
		<td width="32%" height="23">
		  	<nested:text property="address4" size="23" styleClass="txfield" maxlength="50" />
		</td>
	</tr>
	<tr>
		<td width="20%" height="23"><font class="NArialL" >City :</font></td>
	    <td width="20%" height="23">
  		<nested:text property="city" size="23" styleClass="txfield" maxlength="50" />
	    </td>
		<td width="8%" height="23"></td>
	    <td width="20%" height="23"><font class="NArialL" >State :</font></td>
		<td width="32%" height="23">
		  	<nested:text property="state" size="23" styleClass="txfield"  maxlength="50" />
  	    </td>
	</tr>
	<tr>
		<td width="20%" height="23"><font class="NArialL" >Zip :</font></td>
	    <td width="20%" height="23">
  			<nested:text property="zipCode" size="23" styleClass="txfield" />
	    </td>
		<td width="8%" height="23"></td>
	    <td width="20%" height="23"><font class="NArialL" >Country :</font></td>
		<td width="32%" height="23">
	  			<nested:text property="country" size="23" styleClass="txfield" maxlength="50" />
  	    </td>
	</tr>
	<% i++; %>
	<!-- END -->
	</nested:iterate>
    <tr><td colspan='5' align="left"><font id="font1"></font></td></tr>
	<tr><td colspan=4></td><td><a href="javascript:addContactPerson();"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" width="53" height="17"></a></td></tr>
	<tr><td colspan=5></td><td>&nbsp;</td></tr>
	<tr><td colspan=5><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20></td></tr>
	<tr><td colspan=5 align="center">
	<a href="javascript:saveContactPerson();">
		 <img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17" onMouseover="window.status='Click to Save Vendor Contact Person';return true" onMouseout="window.status='';return true" title="Click here to Save Vendor Contact Person"></a>
     	  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		  <a href="javascript:cancelContactPerson();">
		 <img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" width="53" height="17"onMouseover="window.status='Click to Cancel Changes to Vendor Details';return true" onMouseout="window.status='';return true" title="Click here to Cancel Changes Vendor details"></a>
     	  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	 <a href="javascript:document.vendorContactPersonForm.reset();"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" width="53" height="17"onMouseover="window.status='Click to Reset Vendor Details';return true" onMouseout="window.status='';return true" title="click here to Reset Vendor details"></a>
	</td></tr>
</table>
<input type="hidden" name="counter" value="<%=i%>">

</html:form>
<BR><BR><BR></TD>
<TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>
</BODY>
</HTML>